package com.wfs.suanfa.nio.demo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author: wfs
 * @Date: 2020/4/24 13:20
 * @Description:
 */
public class SocketChannelTest {

    public static void main(String[] args) {
        try {
            SocketChannel socketChannel=SocketChannel.open();
            socketChannel.connect(new InetSocketAddress("127.0.0.1",8888));
            ByteBuffer buffer=ByteBuffer.allocate(48);
            int read = socketChannel.read(buffer);
            while (true) {
                while (read != -1) {
                    buffer.flip();
                    while (buffer.hasRemaining()) {
                        System.err.println((char) buffer.get());
                    }
                    System.out.println();
                    buffer.clear();
                    read = socketChannel.read(buffer);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
